package com.winning.jsyd.quarzt;

import com.winning.jsyd.entity.business.DmDiabetesReport;
import com.winning.jsyd.entity.business.DmHighbloodVisit;
import com.winning.jsyd.entity.business.MssTestMaster;
import com.winning.jsyd.quarzt.job.*;
import org.apache.commons.lang.StringUtils;
import org.quartz.*;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: xkh@winning.com.cn
 * @Desc: TODO
 * @Date: 2019-03-21 14:39
 **/
@Configuration
@ConfigurationProperties(prefix = "cron.quzrzt", ignoreInvalidFields = true, ignoreUnknownFields = true)
public class ConfigureQuarzt {

    private JobDataMap jobDataMap = new JobDataMap();

    private Map cronCollection = new HashMap();

    @Autowired
    ConfigureQuarzt(ApplicationContext applicationContext) {
        jobDataMap.put("applicationContext", applicationContext);
    }

    @Bean
    JobDetail haPersonInfoDetail() {
        return createJobDetail(HaPersonInfoJob.class, jobDataMap, "HaPersonInfo_job", "group0");
    }

    @Bean
    Trigger haPersonInfoTrg() throws ParseException {
        return createTrigger("HaPersonInfo_job", "group0", "HaPersonInfo_trg", "group0", String.valueOf(cronCollection.get("HaPersonInfo")));
    }

    @Bean
    JobDetail dmHighbloodReportDetail() {
        return createJobDetail(DmHighbloodReportJob.class, jobDataMap, "DmHighbloodReport_job", "group0");
    }

    @Bean
    Trigger dmHighbloodReportTrg() throws ParseException {
        return createTrigger("DmHighbloodReport_job", "group0", "DmHighbloodReport_trg", "group0", String.valueOf(cronCollection.get("DmHighbloodReport")));
    }

    @Bean
    JobDetail dmDiabetesReportDetail() {
        return createJobDetail(DmDiabetesReportJob.class, jobDataMap, "DmDiabetesReport_job", "group0");
    }

    @Bean
    Trigger dmDiabetesReportTrg() throws ParseException {
        return createTrigger("DmDiabetesReport_job", "group0", "DmDiabetesReport_trg", "group0", String.valueOf(cronCollection.get("DmDiabetesReport")));
    }

    @Bean
    JobDetail dmHighbloodVisitDetail() {
        return createJobDetail(DmHighbloodVisitJob.class, jobDataMap, "DmHighbloodVisit_job", "group0");
    }

    @Bean
    Trigger dmHighbloodVisitTrg() throws ParseException {
        return createTrigger("DmHighbloodVisit_job", "group0", "DmHighbloodVisit_trg", "group0", String.valueOf(cronCollection.get("DmHighbloodVisit")));
    }

    @Bean
    JobDetail dmDiabetesVisitDetail() {
        return createJobDetail(DmDiabetesVisitJob.class, jobDataMap, "DmDiabetesVisit_job", "group0");
    }

    @Bean
    Trigger dmDiabetesVisitTrg() throws ParseException {
        return createTrigger("DmDiabetesVisit_job", "group0", "DmDiabetesVisit_trg", "group0", String.valueOf(cronCollection.get("DmDiabetesVisit")));
    }

    @Bean
    JobDetail mssRcptMasterDetail() {
        return createJobDetail(MssRcptMasterJob.class, jobDataMap, "MssRcptMaster_job", "group0");
    }

    @Bean
    Trigger mssRcptMasterTrg() throws ParseException {
        return createTrigger("MssRcptMaster_job", "group0", "MssRcptMaster_trg", "group0", String.valueOf(cronCollection.get("MssRcptMaster")));
    }

    @Bean
    JobDetail mssOutDiagnosisDetail() {
        return createJobDetail(MssOutDiagnosisJob.class, jobDataMap, "MssOutDiagnosis_job", "group0");
    }

    @Bean
    Trigger mssOutDiagnosisTrg() throws ParseException {
        return createTrigger("MssOutDiagnosis_job", "group0", "MssOutDiagnosis_trg", "group0", String.valueOf(cronCollection.get("MssOutDiagnosis")));
    }

    @Bean
    JobDetail mssPrescMasterDetail() {
        return createJobDetail(MssPrescMasterJob.class, jobDataMap, "MssPrescMaster_job", "group0");
    }

    @Bean
    Trigger mssPrescMasterTrg() throws ParseException {
        return createTrigger("MssPrescMaster_job", "group0", "MssPrescMaster_trg", "group0", String.valueOf(cronCollection.get("MssPrescMaster")));
    }

    @Bean
    JobDetail mssExamMasterDetail() {
        return createJobDetail(MssExamMasterJob.class, jobDataMap, "MssExamMaster_job", "group0");
    }

    @Bean
    Trigger mssExamMasterTrg() throws ParseException {
        return createTrigger("MssExamMaster_job", "group0", "MssExamMaster_trg", "group0", String.valueOf(cronCollection.get("MssExamMaster")));
    }

    @Bean
    JobDetail mssTestMasterDetail() {
        return createJobDetail(MssTestMasterJob.class, jobDataMap, "MssTestMaster_Job", "group0");
    }

    @Bean
    Trigger mssTestMasterTrg() throws ParseException {
        return createTrigger("MssTestMaster_Job", "group0", "MssTestMaster_trg", "group0", String.valueOf(cronCollection.get("MssTestMaster")));
    }

    @Bean
    JobDetail mssPatsBillDetail() {
        return createJobDetail(MssPatsBillDetailJob.class, jobDataMap, "MssPatsBillDetail_Job", "group0");
    }

    @Bean
    Trigger mssPatsBillDetailTrg() throws ParseException {
        return createTrigger("MssPatsBillDetail_Job", "group0", "MssPatsBillDetail_trg", "group0", String.valueOf(cronCollection.get("MssPatsBillDetail")));
    }

    @Bean
    JobDetail mssPatsSettleMasterDetail() {
        return createJobDetail(MssPatsSettleMasterJob.class, jobDataMap, "MssPatsSettleMaster_Job", "group0");
    }

    @Bean
    Trigger mssPatsSettleMasterTrg() throws ParseException {
        return createTrigger("MssPatsSettleMaster_Job", "group0", "MssPatsSettleMaster_trg", "group0", String.valueOf(cronCollection.get("MssPatsSettleMaster")));
    }

    @Bean
    JobDetail mssInOrderDetail() {
        return createJobDetail(MssInOrderJob.class, jobDataMap, "MssInOrder_Job", "group0");
    }

    @Bean
    Trigger mssInOrderTrg() throws ParseException {
        return createTrigger("MssInOrder_Job", "group0", "MssInOrder_trg", "group0", String.valueOf(cronCollection.get("MssInOrder")));
    }

    @Bean
    JobDetail hMssPrescMasterDetail() {
        return createJobDetail(HMssPrescMasterJob.class, jobDataMap, "HMssPrescMaster_Job", "group0");
    }

    @Bean
    Trigger hMssPrescMasterTrg() throws ParseException {
        return createTrigger("HMssPrescMaster_Job", "group0", "HMssPrescMaster_trg", "group0", String.valueOf(cronCollection.get("HMssPrescMaster")));
    }

    @Bean
    JobDetail mssOperationApplyDetail() {
        return createJobDetail(MssOperationApplyJob.class, jobDataMap, "MssOperationApply_Job", "group0");
    }

    @Bean
    Trigger mssOperationApplyTrg() throws ParseException {
        return createTrigger("MssOperationApply_Job", "group0", "MssOperationApply_trg", "group0", String.valueOf(cronCollection.get("MssOperationApply")));
    }

    @Bean
    JobDetail mssResidentAdminDetail() {
        return createJobDetail(MssResidentAdminJob.class, jobDataMap, "MssResidentAdmin_Job", "group0");
    }

    @Bean
    Trigger mssResidentAdminTrg() throws ParseException {
        return createTrigger("MssResidentAdmin_Job", "group0", "MssResidentAdmin_trg", "group0", String.valueOf(cronCollection.get("MssResidentAdmin")));
    }

    @Bean
    JobDetail mssOperationRecordDetail() {
        return createJobDetail(MssOperationRecordJob.class, jobDataMap, "MssOperationRecord_Job", "group0");
    }

    @Bean
    Trigger mssOperationRecordTrg() throws ParseException {
        return createTrigger("MssOperationRecord_Job", "group0", "MssOperationRecord_trg", "group0", String.valueOf(cronCollection.get("MssOperationRecord")));
    }

    @Bean
    JobDetail mssDischargeDetail() {
        return createJobDetail(MssDischargeJob.class, jobDataMap, "MssDischarge_Job", "group0");
    }

    @Bean
    Trigger mssDischargeTrg() throws ParseException {
        return createTrigger("MssDischarge_Job", "group0", "MssDischarge_trg", "group0", String.valueOf(cronCollection.get("MssDischarge")));
    }






    /**
     * @param job        作业的Job的Class对象
     * @param jobDataMap job需要的数据集
     * @param jobname    job名称
     * @param jobgroup   job所属组
     * @return 新的JobDetail实例
     */
    private JobDetail createJobDetail(Class<? extends Job> job, JobDataMap jobDataMap, String jobname, String jobgroup) {

        if (Objects.isNull(job) || StringUtils.isEmpty(jobname)) {
            throw new IllegalArgumentException("[job、jobname] arguments must not be empty");
        }

        JobBuilder jobBuilder = JobBuilder.newJob(job);

        if (StringUtils.isNotEmpty(jobgroup)) {
            jobBuilder.withIdentity(jobname, jobgroup);
        } else {
            jobBuilder.withIdentity(jobname);
        }

        if (Objects.nonNull(jobDataMap)) {
            jobBuilder.usingJobData(jobDataMap);
        }

        return jobBuilder.storeDurably(true).build();
    }


    /**
     * @param jobname  job名称
     * @param jobgroup job所属组
     * @param trgname  触发器名称
     * @param trggroup 触发器所属组
     * @param cron     cron定时时间模式
     * @return 新的触发器对象
     */
    private Trigger createTrigger(String jobname, String jobgroup, String trgname, String trggroup, String cron) throws ParseException {

        if (StringUtils.isEmpty(jobname)
                || StringUtils.isEmpty(trgname)
                || StringUtils.isEmpty(cron)) {
            throw new IllegalArgumentException("[jobname、trgname、cron] arguments must not be empty");
        }

        CronTriggerImpl trigger = new CronTriggerImpl();
        trigger.setJobName(jobname);
        trigger.setName(trgname);
        trigger.setCronExpression(cron);

        if (StringUtils.isNotEmpty(jobgroup)) {
            trigger.setJobGroup(jobgroup);
        }

        if (StringUtils.isNotEmpty(trggroup)) {
            trigger.setGroup(trggroup);
        }

        return trigger;
    }


    public Map getCronCollection() {
        return cronCollection;
    }
}
